package com.liruo.learn.spring.mvc.config;

import com.liruo.learn.spring.mvc.filter.FilterTest1;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.core.annotation.Order;

import javax.servlet.*;
import java.io.IOException;
import java.util.stream.Stream;

/**
 * @Author:liruo
 * @Date:2023-03-09-23:10:28
 * @Desc
 */
//@ServletComponentScan("com.liruo.learn.spring.mvc.filter")
//@Configuration
@Slf4j
public class PrimitiveServletConfig {
    @Order(10)
    public static class TestFilter3Bean implements Filter {
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
            log.info("{} init", FilterTest1.class.getName());
        }
        @Override
        public void destroy() {
            log.info("{} destroy", FilterTest1.class.getName());
        }

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//            log.info("{} doFilter", FilterTest1.class.getName());
            filterChain.doFilter(servletRequest, servletResponse);
//            log.info("{} come back to doFilter", FilterTest1.class.getName());
        }
    }
    @Bean
    public FilterRegistrationBean<TestFilter3Bean> testFilter3Bean(){
        FilterRegistrationBean<TestFilter3Bean> bean = new FilterRegistrationBean<>();
        bean.setFilter(new TestFilter3Bean());
        bean.setUrlPatterns(Stream.of("/*").toList());
        return bean;
    }
}
